package cn.com.xinjinfu.pdoulcProduct.utils;

import android.app.Activity;
import android.content.Context;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;

import java.math.BigDecimal;
import java.util.Timer;
import java.util.TimerTask;

/**
 * Double类型的EditText专用类，
 * 截取到小数点后两位，
 * 获取Double类型的EditText值
 * 比较Double类型值大小
 * Created by android-adt on 2016/1/6.
 */
public class EditTextUtil {

    private Activity context;
    public EditTextUtil(Activity context) {
        this.context = context;
    }
    /**
     * 设置EditText只能输入小数点后两位
     * @param editText
     */
    public static void setPricePoint(final EditText editText) {
        editText.addTextChangedListener(new TextWatcher() {

            @Override
            public void onTextChanged(CharSequence s, int start, int before,
                                      int count) {
                if (s.toString().contains(".")) {
                    if (s.length() - 1 - s.toString().indexOf(".") > 2) {
                        s = s.toString().subSequence(0,
                                s.toString().indexOf(".") + 3);
                        editText.setText(s);
                        editText.setSelection(s.length());
                    }
                }
                if (s.toString().trim().substring(0).equals(".")) {
                    s = "0" + s;
                    editText.setText(s);
                    editText.setSelection(2);
                }

                if (s.toString().startsWith("0")
                        && s.toString().trim().length() > 1) {
                    if (!s.toString().substring(1, 2).equals(".")) {
                        editText.setText(s.subSequence(0, 1));
                        editText.setSelection(1);
                        return;
                    }
                }
            }

            @Override
            public void beforeTextChanged(CharSequence s, int start, int count,
                                          int after) {
            }

            @Override
            public void afterTextChanged(Editable s) {
                // TODO Auto-generated method stub

            }

        });

    }

    /**
     * 取得EditText值String类型
     * @param editText
     * @return
     */
    public static String getStringEditText(final EditText editText){
        return editText.getText().toString();
    }
    /**
     * 取得EditText值double类型
     * @param editText
     * @return
     */
    public static double getDoubleEditText(final EditText editText){
        String mStringEditText = getStringEditText(editText);
        if (!"".equals(mStringEditText)) {
            return Double.parseDouble(mStringEditText);
        }
        return 0;
    }
    /**
     * 取得EditText值int类型
     * @param editText
     * @return
     */
    public static int getIntEditText(final EditText editText){
        String mStringEditText = getStringEditText(editText);
        if (!"".equals(mStringEditText)) {
            return Integer.parseInt(mStringEditText);
        }
        return 0;
    }
    /**
     * 比较输入和余额的值的大小
     * @param getValue    输入的值
     * @param aliveValue  余额
     * @return
     */
    public static boolean compareSize(double getValue,double aliveValue){
        BigDecimal data1 = new BigDecimal(getValue);
        BigDecimal data2 = new BigDecimal(aliveValue);
        if (data1.compareTo(data2) <= 0) {
            return true;
        }
        return false;
    }

    public static void setUpSofe(final EditText mPetPwd){
        mPetPwd.setFocusable(true);
        mPetPwd.setFocusableInTouchMode(true);
        mPetPwd.requestFocus();
        Timer timer = new Timer();
        timer.schedule(new TimerTask()
                       {
                           public void run()
                           {
                               InputMethodManager inputManager =
                                       (InputMethodManager) mPetPwd.getContext().getSystemService(Context.INPUT_METHOD_SERVICE);
                               inputManager.showSoftInput(mPetPwd, 0);
                           }
                       },
                200);
    }


}
